package com.ruoyi.repast.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.repast.entity.BPrivateRoom;
import com.ruoyi.repast.service.BPrivateRoomService;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.plugin.spring.boot.service.BaseService;
import org.nutz.plugin.spring.boot.service.entity.Pager;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * @author zhangjialu
 * @date 2021/7/31 下午11:23
 */
@Service
public class BPrivateRoomServiceImpl extends BaseService<BPrivateRoom> implements BPrivateRoomService {

    @Override
    public TableDataInfo listPage(BPrivateRoom bPrivateRoom,Integer pageNum,Integer pageSize) {
        Pager<BPrivateRoom> pager= this.searchByPage(pageNum,pageSize, Cnd.NEW().and("delFlag","=","0"));

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(pager.getDataList());
        rspData.setTotal(pager.getPageSize());

        return rspData;
    }

    @Override
    public AjaxResult saveBPrivateRoom(BPrivateRoom bPrivateRoom) {
        Date currentDate = new Date();
        if(StringUtils.isBlank(bPrivateRoom.getId())){
            bPrivateRoom.setCreateTime(currentDate);
            bPrivateRoom.setUpdateTime(currentDate);
            bPrivateRoom.setDelFlag("0");
            this.save(bPrivateRoom);
            if(StringUtils.isNotEmpty(bPrivateRoom.getId())){
                return AjaxResult.success();
            }
        }else{
            bPrivateRoom.setUpdateTime(currentDate);
            int count = this.updateIgnoreNull(bPrivateRoom);
            if(count>0){
                return AjaxResult.success();
            }
        }
        return AjaxResult.error();
    }

    @Override
    public Object selectJobById(String bPrivateRoomId) {
        return this.fetch(bPrivateRoomId);
    }

    @Override
    public void deleteBPrivateRoomByIds(String ids) {
        if(StringUtils.isNoneEmpty(ids)){
            String idList[] = ids.split(",");
            for(int i=0;i<idList.length;i++){
                this.dao().update(BPrivateRoom.class, Chain.make("delFlag","1"),Cnd.NEW().and("id","=",idList[i]));
            }
        }
    }
}
